package com.cheetah.start.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.common.base.result.ResultTree;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.module.system.domain.SysMenu;
import com.cheetah.start.module.system.domain.SysRealMenu;
import com.cheetah.start.module.system.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Valid;

import java.util.List;


/**
 * 系统菜单控制器
 *
 * @author Feel
 * @date 2021/08/22
 */
@Slf4j
@Valid
@Controller
@Mapping("/menu")
public class SysMenuController extends ResultController {
    @Inject
    SysMenuService sysMenuService;

    /**
     * 编辑页面
     *
     * @param menuId 菜单id
     * @return {@link ModelAndView}
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit(Integer menuId) {
        SysMenu menu = sysMenuService.getById(menuId);
        ModelAndView modelAndView = jumpPage("/menu/edit");
        modelAndView.put("sysMenu", menu);
        return modelAndView;
    }

    /**
     * 用户菜单
     *
     * @return {@link SysRealMenu}
     */
    @Get
    @Mapping("/userMenu")
    public List<SysRealMenu> userMenu() {
        int userId = StpUtil.getLoginIdAsInt();
        List<SysRealMenu> sysRealMenus = sysMenuService.menuList(userId);
        sysMenuService.setPreFix(sysRealMenus, "");
        return sysMenuService.toMenu(sysRealMenus, "0");
    }


    /**
     * 父
     *
     * @return {@link ResultTree}
     */
    @Get
    @Mapping("/selectParent")
    public ResultTree parent() {
        List<SysMenu> list = sysMenuService.list();
        SysMenu sysMenu = new SysMenu();
        sysMenu.setMenuName("顶级权限").setMenuId(0).setParentId(-1);
        list.add(sysMenu);
        return dataTree(list);
    }

    /**
     * 禁用
     *
     * @param menuId 菜单id
     * @return {@link Result}
     */
    @Put
    @SaCheckPermission(value = "sys:menu:switch", orRole = "admin")
    @Mapping("/disable")
    public Result disable(@NotNull(message = "ID不能为空") Integer menuId) {
        return sysMenuService.enableOrDisable(menuId, "0");
    }


    /**
     * 启用
     *
     * @param menuId 菜单id
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:menu:switch", orRole = "admin")
    @Mapping("/enable")
    @Put
    public Result enable(@NotNull(message = "ID不能为空") Integer menuId) {
        return sysMenuService.enableOrDisable(menuId, "1");
    }

    /**
     * 添加
     *
     * @param sysMenu 系统菜单
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:menu:add", orRole = "admin")
    @Mapping("/add")
    @Post
    public Result add(SysMenu sysMenu) {
        sysMenu.setEnable("1");
        return decide(sysMenuService.save(sysMenu), BusinessTypeEnum.ADD);
    }

    /**
     * 删除
     *
     * @param menuId 菜单id
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:menu:delete", orRole = "admin")
    @Mapping("/deleteOne")
    @Delete
    public Result delete(@NotNull(message = "ID不能为空") Integer menuId) {
        return decide(sysMenuService.removeById(menuId), BusinessTypeEnum.REMOVE);
    }


    /**
     * 更新
     *
     * @param sysMenu 系统菜单
     * @return {@link Result}
     */
    @SaCheckPermission(value = "sys:menu:edit", orRole = "admin")
    @Mapping("/update")
    @Post
    public Result update(@Body SysMenu sysMenu) {
        if (sysMenu.getParentId() == null) {
            return failure("请选择上级菜单");
        }
        return decide(sysMenuService.updateById(sysMenu), BusinessTypeEnum.EDIT);
    }


    /**
     * 列表
     *
     * @param cond parent_id
     * @return {@link ResultTable}
     */
    @SaCheckPermission(value = "sys:menu:data", orRole = "admin")
    @Mapping("/list")
    @Get
    public ResultTable list(String cond) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(cond)) {
            wrapper.eq("parent_id", cond);
        }
        wrapper.orderByAsc("sort");
        List<SysMenu> list = sysMenuService.list(wrapper);
        return treeTable(list);
    }

}
